'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 领取联盟击杀BOSS奖励
 * @author agl
 */

var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var atkData = require('../../../../libs/war/atkData');
var unionBossData = require('../../../../libs/war/unionBossData');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService.js');
var rewardService = require('../../../../services/rewardService.js');
var unionGroupModel = require('../../../../models/unionGroupModel.js');
var unionBossModel = require('../../../../models/unionBossModel.js');
var unionBossConf = require('../../../../config/unionBossConf.js');

/**
 * 领取联盟击杀BOSS奖励
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var total_damage = msg.total_damage;

    var atk_data;
    var def_data;

    var union_group_model;
    var union_boss_model;
    var union_id;
    var reward_list = [];

    if (!total_damage) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'role_union_boss', 'task', 'build', 'soldier', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var role_union_boss_model = player_manager.role_union_boss;

                union_id = role_model.union_id;
                if (union_id === 0) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }

                if (role_union_boss_model.total_damage < total_damage) {
                    // 尚未开启的BOSS
                    return next(null, {
                        code: Code.UNIOIN_BOSS_NOT_REACH_DAMAGE_LEVEL
                    });
                }

                if (!role_union_boss_model.addToTotalDamageRewardList(total_damage)) {
                    // 奖励已领取过
                    return next(null, {
                        code: Code.UNIOIN_BOSS_ALREADY_PICKED_REWARD
                    });
                }

                // 发送奖励
                reward_list = unionBossConf.getTotalDamageReward(total_damage);
                if (!reward_list) {
                    return next(null, {
                        code: Code.UNIOIN_BOSS_INVALID_DAMAGE_LEVEL
                    });
                }
                var module_type = moduleType.MDL_UNION_BOSS;
                var operation_type = operationType.OPR_UNION_BOSS_TOTAL_DAMAGE_REWARD;
                var res_int_val = total_damage;
                rewardService.addRewardListToRole(player_manager, reward_list, module_type, operation_type, res_int_val);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
